package com.hennissy.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

/**
 * <p>
 *Swagger配置文件
 * </p>
 *
 * @author cjh
 * @date 2021/2/23 11:48
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket docket1(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("group1");
    }
    @Bean
    public Docket docket2(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("group2");
    }
    @Bean
    public Docket docket3(){
        return new Docket(DocumentationType.SWAGGER_2).groupName("group3");
    }

    //配置了swagger的Docket的bean实例
    @Bean
    public Docket docket(Environment environment) {
        // 设置要显示swagger的yml环境
        Profiles of = Profiles.of("dev");
        // 判断当前是否处于该环境
        // 通过 enable() 接收此参数判断是否要显示
        boolean b = environment.acceptsProfiles(of);

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .groupName("cjh")
                .enable(b) //配置是否启用Swagger，如果是false，在浏览器将无法访问
                .select()// 通过.select()方法，去配置扫描接口,RequestHandlerSelectors配置如何扫描接口
                .apis(RequestHandlerSelectors.basePackage("com.hennissy.controller"))
                // 配置如何通过path过滤,即这里只扫描请求以/xxx开头的接口
                //.paths(PathSelectors.ant("/accountApi/**"))
                .build();
    }

    private ApiInfo apiInfo(){
        //联系人信息
        Contact contact=new Contact("cjh","http://www.baidu.com","1564866653@qq.com");
        return new ApiInfo(
               "cjh的swaggerApi文档",
                "接口文档",
                "1.0",
                "http://www.baidu.com",
                contact,
                "Apache 2.0",
                "http://www.baidu.com",
                new ArrayList<>()
        );
    }
}